﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

namespace SharpGeo.Projections
{
    /// <summary>
    /// Defines a PlateCaree Projection (Equidistance)
    /// </summary>
    public class PlateCarree : Projection
    {
        #region Constructor

        /// <summary>
        /// Constructs a PlateCarree Projection
        /// </summary>
        /// <param name="coordinateReferenceSystem">The underlying coordinate reference system</param>
        public PlateCarree(CoordinateReferenceSystem coordinateReferenceSystem)
            : base(coordinateReferenceSystem)
        {
        }

        #endregion Constructor

        #region Override Methods

        /// <summary>
        /// Projects the passed ICoordinate to the screen (X,Y) using the PlateCarree Projection
        /// </summary>
        /// <param name="coordinate">The ICoordinate to project</param>
        /// <param name="mapWidth">The map width</param>
        /// <param name="mapHeight">The map height</param>
        /// <param name="transformation">A post projection transformation</param>
        /// <returns>The projected ICoordinate</returns>
        public override ICoordinate ProjectToScreen(
            ICoordinate coordinate,
            double mapWidth, double mapHeight,
            ICoordinateTransformation transformation)
        {
            if (coordinate == null || coordinate.NumberOfAxes < 2)
                return null;

            var sc = new ScreenCoordinate(
                (coordinate[0] + 180D) * (mapWidth / 360),
                (mapHeight / 2D) - (mapHeight * coordinate[1] / 180D));

            ICoordinate tsc = sc;

            if (transformation != null)
            {
                if (transformation.CanTransform(sc))
                {
                    tsc = transformation.Transform(tsc);
                }
            }

            return tsc;
        }

        /// <summary>
        /// Inverse projects the passed ICoordinate from the screen (X,Y) using the PlateCarree Projection
        /// </summary>
        /// <param name="coordinate">The ICoordinate to inverse project</param>
        /// <param name="mapWidth">The map width</param>
        /// <param name="mapHeight">The map height</param>
        /// <param name="transformation">A pre inverse projection transformation</param>
        /// <returns>The inverse projected ICoordinate</returns>
        public override ICoordinate InverseProjectFromScreen(
            ICoordinate coordinate,
            double mapWidth, double mapHeight,
            ICoordinateTransformation transformation)
        {
            if (coordinate == null || coordinate.NumberOfAxes < 2)
                return null;

            var c = coordinate;

            if (transformation != null)
            {
                if (transformation.CanInverseTransform(c))
                {
                    c = transformation.InverseTransform(c);
                }
            }

            var x = (((double)c[0] / mapWidth) * 360D) - 180D;
            var y = 90D - (((double)c[1] / mapHeight) * 180D);
            return new GeographicCoordinate(x, y);
        }

        #endregion Override Methods
    }
}